package com.sskj.common.box.okgointeceptor;

import com.lzy.okgo.utils.EncryptUtils;
import com.sskj.common.util.GSonUtil;
import com.sskj.common.util.MD5Util;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import okhttp3.Cookie;
import okhttp3.CookieJar;
import okhttp3.FormBody;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

import static com.lzy.okgo.utils.EncryptUtils.base64Decode;
import static com.lzy.okgo.utils.EncryptUtils.base64Encode;

/**
 * 作者 :吕志豪
 * 简书：https://www.jianshu.com/u/6e525b929aac
 * github：https://github.com/lvzhihao100
 * 描述：
 * 创建时间：2018-09-13 19:09
 */
public class CookieInterceptor implements Interceptor {
    private final CookieJar cookieJar;

    public CookieInterceptor(CookieJar cookieJar) {
        this.cookieJar = cookieJar;
    }

    private String privateKey = "12";

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request userRequest = chain.request();


        Map<String, String> paramMap = new HashMap<>();
        if (userRequest.method().equals("GET")) {
            HttpUrl url = userRequest.url();
            Set<String> names = url.queryParameterNames();
            Iterator<String> iterator = names.iterator();
            while (iterator.hasNext()) {
                String name = iterator.next();
                List<String> values = url.queryParameterValues(name);
                if (values != null && values.size() > 0 && values.get(0) != null) {
                    paramMap.put(name, values.get(0));
                }
            }
            HttpUrl newUrl = url.newBuilder().addEncodedQueryParameter("data", encrypt(paramMap)).build();

            userRequest.newBuilder().url(newUrl).build();
            return chain.proceed(userRequest);

        } else {//POST
            //参数就要针对body做操作,我这里针对From表单做操作
            if (userRequest.body() instanceof FormBody) {
                // 构造新的请求表单
                FormBody body = (FormBody) userRequest.body();
                //将以前的参数添加
                for (int i = 0; i < body.size(); i++) {
                    paramMap.put(body.encodedName(i), body.encodedValue(i));

                }

                FormBody.Builder formBuilder = new FormBody.Builder();
                //追加新的参数
                formBuilder.add("data", encrypt(paramMap));

                userRequest = userRequest.newBuilder().post(formBuilder.build()).build();//构造新的请求体

            }
            return chain.proceed(userRequest);

        }


//        List<Cookie> cookies = cookieJar.loadForRequest(userRequest.url());
//        for (Cookie cookie : cookies) {
//            requestBuilder.header(cookie.name(), cookie.value());
//        }
    }

    private String encrypt(Map<String, String> map) {
        String string = GSonUtil.GsonString(map);
        return new String(EncryptUtils.encryptRSA2Base64(string.getBytes(), base64Decode(privateKey.getBytes()), false, "RSA/ECB/PKCS1Padding"));
    }
}
